﻿CREATE PROCEDURE [dbo].[proc_InOutSide_TongJiView]
	(
		@DepartmentId int,
		@StartDate nvarchar(10),
		@EndDate nvarchar(10),
		@StartIndex int,
		@EndIndex int
	)
AS
Begin
	;WITH list As (Select  ROW_NUMBER() OVER (ORDER BY If_Date DESC)AS Row,
		[Id]
      ,[CusId]
      ,(select CustomerName from Customer where Customer.CustomerId=InOutSide.CusId) as CustomerName
      ,[CompanyId]
      ,(SELECT Company.CompanyName FROM Company WHERE Company.Id=InOutSide.CompanyId) AS CompanyName
      ,[InSide]
      ,(SELECT Employee.EName FROM Employee WHERE UserName=InOutSide.InSide) InSideName
      ,[In_Date]
      ,[InSide_ProtectDate]
      ,[CC_Id]
      ,[IfSide]
      ,ISNULL((SELECT Employee.EName FROM Employee WHERE UserName=InOutSide.IfSide),'') IfSideName
      ,[If_Date]
      ,[OutSide]
      ,ISNULL((SELECT Employee.EName FROM Employee WHERE UserName=InOutSide.OutSide),'') OutSideName
      ,[Out_Date]
      ,[Out_Memo]
      ,[DepId]
      ,Case When [Out_Date] Is Null Then 0 Else 1 End As Out_DateFlag
	From InOutSide 
	Where DepId=@DepartmentId AND If_Date IS NOT NULL and Convert(nchar(10),If_Date,120)>=@StartDate and Convert(nchar(10),If_Date,120)<=@EndDate
	)
	--Select * From list Where Row between @StartIndex and @EndIndex
	Select *,(Select Count(0) From list) As RecordCount From list Where Row between @StartIndex and @EndIndex Order By Row
End
